package dao.impl;

import dao.UserDao;
import org.apache.ibatis.session.SqlSession;
import pojo.User;
import util.MyBatisUtil;

import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public List<User> findAll(User user) {
        return MyBatisUtil.getSession().getMapper(UserDao.class).findAll(user);
    }

    @Override
    public int addUser(User user) {
        SqlSession session = MyBatisUtil.getSession();
        int result = session.getMapper(UserDao.class).addUser(user);
        session.commit();
        return result;
    }

    @Override
    public User findById(Integer id) {
        return MyBatisUtil.getSession().getMapper(UserDao.class).findById(id);

    }

    @Override
    public int updateUser(User user) {
        SqlSession session = MyBatisUtil.getSession();
        int result = session.getMapper(UserDao.class).updateUser(user);
        session.commit();
        return result;
    }

    @Override
    public int deleteUserById(Integer id) {
        SqlSession session = MyBatisUtil.getSession();
        int result = session.getMapper(UserDao.class).deleteUserById(id);
        session.commit();
        return result;
    }
}
